package gui.one;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.management.RuntimeErrorException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import car.beans.CarInfo;
import service.QueryServiceImpl;
import service.QueryServiceInter;

public class Expr extends JFrame
{

	private JPanel contentPane;
	private JTextField input;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					Expr frame = new Expr();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Expr()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setBounds(10, 10, 414, 251);
		splitPane.setResizeWeight(0.5);
		contentPane.add(splitPane);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(null);
		
		input = new JTextField();
		input.setBounds(71, 5, 122, 21);
		panel.add(input);
		input.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("input");
		lblNewLabel.setBounds(31, 8, 30, 15);
		panel.add(lblNewLabel);
		
		JComboBox c = new JComboBox();
		c.setModel(new DefaultComboBoxModel(new String[] {"品牌", "型号", "马力", "颜色"}));
		c.setBounds(27, 49, 166, 23);
		panel.add(c);
		
		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id = input.getText().trim();
				if(id.length() == 0)
					throw new RuntimeErrorException(new Error("没有输入任何字符"));
				else
				{
					QueryServiceInter qsi = new QueryServiceImpl("mysql");
					String type = String.valueOf(c.getItemAt(c.getSelectedIndex()));
					List<CarInfo> lst = qsi.selectById(id, type);
					String[] col = {"品牌", "型号", "马力", "颜色"};
					DefaultTableModel dm = new DefaultTableModel(col, 0);
					for(CarInfo ci: lst)
					{
						String[] s = {ci.getBrand(), ci.getModel(), ci.getHp(), ci.getColor()};
						dm.addRow(s);
					}
					table.setModel(dm);
				}
			}
		});
		button.setBounds(36, 216, 93, 23);
		panel.add(button);
		
		JPanel panel_1 = new JPanel();
		splitPane.setRightComponent(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 184, 229);
		panel_1.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
	}
}
